package com.jcl.core.security;

import com.alibaba.fastjson.JSON;
import com.jcl.core.exception.AuthorityException;
import com.jcl.core.utils.EncryptUtils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Date;

/**
 * @ClassName SecurityUtils
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/20 19:25
 * @Version 1.0
 **/
public class SecurityUtils {

    public static final String FROM_WEB                        = "web";

    public static final String FROM_WECHAT                     = "wechat";

    public static final String FROM_WECHAT_SCREEN              = "wechat_screen";

    public static final String FROM_PERMISS                    = "permiss";

    public static void main(String[] args) {
        String dd = encodeUserToken("JCL0000001", "100006",",0,1,",new Date(), FROM_WECHAT);
        // System.out.println(dd);

        //UserToken uer = decodeUserToken("AD7A991D76EB0B08CE7950AB63BAD7F4FC4A7C7634120F1C9416DD50AEDAA732D5FBA753230DFEAEFD980CBA6058389BE31DFD0338581A81C95AAE415085114277881B864C8716D35B873F79454C44C65C3A04F6E1B2855B1AEF5FA392DDA2EBF668B39CCE8DA6905A378B8C376474A455EC28567E00298E3B824A6354F728CDEE7CBB2951E90F8F9E5AB5A3DAD0B73A016E2DD38DA0AB9CC92C04FEE0C8C374A9ADACEFA17B6636");

        System.out.println(1);
    }

    public static String returnErrorJsonStr(String code, String msg){
        StringBuffer buffer = new StringBuffer("{");
        buffer.append("\"code\":").append(code).append("\",");
        buffer.append("\"msg\":\"").append(msg).append("\",");
        buffer.append("\"type\":\"error\"");
        buffer.append("}");
        return buffer.toString();
    }

    public static UserToken decodeUserToken(String token) throws AuthorityException {
        String tempToken = null;
        try {
            tempToken = EncryptUtils.simpleDecode(token);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        // 转码
        byte[] asBytes = Base64.getDecoder().decode(tempToken);
        String unionCode = null;
        try {
            unionCode = new String(asBytes, "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new AuthorityException(RequestStatus.REQUEST_ILLEGAL_EN);
        }
        UserToken userToken = JSON.parseObject(unionCode, UserToken.class);
        return userToken;
    }


    public static String encodeUserToken(UserToken userToken){
        String json = jsonUserToken(userToken);
        // 编码
        String unionCode = null;
        try {
            unionCode = Base64.getEncoder().encodeToString(
                    json.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return EncryptUtils.simpleEncode(unionCode);
    }

    public static String encodeUserToken(String corpCode,
                                         String userId,
                                         String roles,
                                         Date abateDate,
                                         String fromType){
        UserToken userToken = new UserToken();
        userToken.setCorpCode(corpCode);
        userToken.setUserId(userId);
        userToken.setRoles(roles);
        if(null != abateDate){
            userToken.setAbateDate(abateDate.getTime());
        }
        userToken.setFromType(fromType);
        userToken.setTimestamp(System.currentTimeMillis() / 1000);
        return encodeUserToken(userToken);
    }

    public static String jsonUserToken(UserToken userToken){
        return JSON.toJSONString(userToken);
    }

}
